package snev_cr;

import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.concurrent.BlockingQueue;

public class Sender implements Runnable {

	BlockingQueue<SeedContainer> cola;
	Integer port;
	
	public Sender(int port, BlockingQueue<SeedContainer> queue)
	{

		this.cola = queue;
		this.port = port;
	}
	

	public void run() 
	{
	
		//TODO cambiar orden 
		try {
			ServerSocket sock = new ServerSocket(port);
			
			while (true)
			{
				Socket conection = sock.accept();
				System.out.println("sender enviando seeds");
				PrintWriter outToClient = new PrintWriter(conection.getOutputStream(), true);

				if (cola.size() >0)
				{
					SeedContainer seed = cola.peek();
					if (seed.fecha.compareTo(Calendar.getInstance().getTime()) > 0 && seed.seed2 != -1)
					{
						outToClient.println(String.valueOf(seed.seed1) + "," + 
								String.valueOf(seed.seed2));
						
					
					}
					else
					{
						outToClient.println(String.valueOf(seed.seed1));
					}
				}

				conection.close();
			}
		} catch (Exception  e) {
			System.out.println("error en sender de code tranceptor");
		}
		
		
	}
	
	
}
